<template>
  <div class="goods-comment">
    <div class="head" v-if="commentInfo">
      <div class="data">
        <p><span>{{ commentInfo.salesCount }}</span><span>人购买</span></p>
        <p><span>{{ commentInfo.praisePercent }}</span><span>好评率</span></p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd">
          <a @click="changeTag(tag, index)" :class="{ active: activeTagIndex === index }"
            v-for="(tag, index) in commentInfo.tags" :key="tag.title" href="javascript:;">{{
              tag.title }}（{{ tag.tagCount }}）</a>
        </div>
      </div>
    </div>
    <div class="sort">
      <span>排序：</span>
      <a @click="changeSort(null)" href="javascript:;" :class="{ active: reqParams.sortField === null }">默认</a>
      <a @click="changeSort('createTime')" href="javascript:;"
        :class="{ active: reqParams.sortField === 'createTime' }">最新</a>
      <a @click="changeSort('praiseCount')" href="javascript:;"
        :class="{ active: reqParams.sortField === 'praiseCount' }">最热</a>
    </div>
    <!-- 列表 -->
    <div class="list" v-if="commentList.length">
      <div v-for="item in commentList" :key="item.id" class="item">
        <div class="user">
          <img :src="item.member.avatar" alt="">
          <span>{{ formatNickname(item.member.nickname) }}</span>
        </div>
        <div class="body">
          <div class="score">
            <i v-for="i in item.score" :key="i + '1'" class="iconfont icon-wjx01"></i>
            <i v-for="i in 5 - item.score" :key="i + '2'" class="iconfont icon-wjx02"></i>
            <span class="attr">{{ formatSpecs(item.orderInfo.specs) }}</span>
          </div>
          <div class="text">{{ item.content }}</div>
          <!-- 图片组件 -->
          <GoodsCommentImage :pictures="item.pictures" v-if="item.pictures.length"></GoodsCommentImage>
          <div class="time">
            <span>{{ item.createTime }}</span>
            <span class="zan"><i class="iconfont icon-dianzan"></i>{{ item.praiseCount }}</span>
          </div>
        </div>
      </div>
    </div>
    <!-- 分页 -->
    <xtx-pagination @page-change="pageChangeHandler" :total="total" :currentPage="reqParams.page"
      :pageSize="reqParams.pageSize"></xtx-pagination>
  </div>
</template>
<script setup>
import { inject, reactive, ref, watch } from 'vue'
import { findCommentInfoByGoods, findCommentsList } from '@/api/product'
import GoodsCommentImage from './goods-comment-image.vue'
// 获取数据
const goods = inject('goods')
// 定义关联查询数据的筛选条件
const reqParams = reactive({
  page: 1,
  pageSize: 10,
  hasPicture: false, // 是否有图
  tag: null, // tag标签选项
  sortField: null, // 排序字段，可选值范围[praiseCount,createTime]
  sortMethod: 'desc' // 排序方法，可选值范围[asc,desc],默认为desc
})
// 获取评价数据
const commentInfo = ref(null)
// 获取评价信息的方法
async function getCommentInfo(id) {
  const { result } = await findCommentInfoByGoods(id)
  // 将有图和全部评价都放入到数据
  result.tags.unshift({ type: 'img', title: '有图', tagCount: result.hasPictureCount })
  result.tags.unshift({ type: 'all', title: '全部评价', tagCount: result.evaluateCount })
  commentInfo.value = result
}
// 默认激活的tag标签的索引
const activeTagIndex = ref(0)
// 改变标签选项
function changeTag(tag, index) {
  // 更改选中的tag标签的索引
  activeTagIndex.value = index
  // 更改筛选条件
  if (tag.type === 'all') {
    reqParams.tag = null
    reqParams.hasPicture = false
  } else if (tag.type === 'img') {
    reqParams.hasPicture = true
    reqParams.tag = null
  } else {
    reqParams.tag = tag.title
    reqParams.hasPicture = false
  }
  getCommentList(reqParams, goods.value.id)
}
// 改变排序规则
function changeSort(field) {
  reqParams.sortField = field
  getCommentList(reqParams, goods.value.id)
}
// 获取商品评论列表
// 定义商品评论列表数据
const commentList = ref([])
const total = ref(0)
async function getCommentList(params, id) {
  const { result } = await findCommentsList(params, id)
  console.log(result)
  commentList.value = result.items
  total.value = result.counts
}
// 监听商品id的改变
watch(() => goods.value.id, id => {
  reqParams.page = 1
  reqParams.pageSize = 10
  reqParams.hasPicture = false
  reqParams.tag = null
  reqParams.sortField = null
  // 获取评论信息
  getCommentInfo(goods.value.id)
  // 获取评论列表数据
  getCommentList(reqParams, id)
}, { immediate: true })

// 定义处理specs的方法
function formatSpecs(specs) {
  return specs.reduce((prev, next) => {
    return `${prev} ${next.name}：${next.nameValue}`
  }, '').trim()
}
// 处理昵称的方法
function formatNickname(nickname) {
  return nickname.substr(0, 1) + '****' + nickname.substr(-1)
}
// 改变页码
function pageChangeHandler(newPage) {
  reqParams.page = newPage
  getCommentList(reqParams, goods.value.id)
}

</script>
<style scoped lang="less">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;

    .data {
      width: 340px;
      display: flex;
      padding: 20px;

      p {
        flex: 1;
        text-align: center;

        span {
          display: block;

          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }

          &:last-child {
            color: #999;
          }
        }
      }
    }

    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;

      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }

      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;

        >a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;

          &:hover {
            border-color: @xtxColor;
            background: lighten(@xtxColor, 50%);
            color: @xtxColor;
          }

          &.active {
            border-color: @xtxColor;
            background: @xtxColor;
            color: #fff;
          }
        }
      }
    }
  }

  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;

    >span {
      margin-left: 20px;
    }

    >a {
      margin-left: 30px;

      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }

  .list {
    padding: 0 20px;

    .item {
      display: flex;
      padding: 25px 10px;
      border-bottom: 1px solid #f5f5f5;

      .user {
        width: 160px;

        img {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          overflow: hidden;
        }

        span {
          padding-left: 10px;
          color: #666;
        }
      }

      .body {
        flex: 1;

        .score {
          line-height: 40px;

          .iconfont {
            color: #ff9240;
            padding-right: 3px;
          }

          .attr {
            padding-left: 10px;
            color: #666;
          }
        }
      }

      .text {
        color: #666;
        line-height: 24px;
      }

      .time {
        color: #999;
        display: flex;
        justify-content: space-between;
        margin-top: 5px;
      }
    }
  }
}
</style>